﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using CML.Intercamber.Business;
using CML.Intercamber.Web.Helpers;

namespace CML.Intercamber.Web.Controllers
{
    public class ReferentialController : BaseController
    {

        private class HobbiesDetail
        {
            public string Code { get; set; }
            public string Label { get; set; }
            public List<HobbiesDetail> Children { get; set; }
        }

        private List<HobbiesDetail> RecurseHobbiesList(List<Hobbies> all, string parent, bool useRecurse)
        {
            List<HobbiesDetail> res = all.Where(x => x.CodeHobbyParent == (string.IsNullOrEmpty(parent) ? null : parent)).OrderBy(x => x.OrderHobby).Select(x => new HobbiesDetail {
                Code = x.CodeHobby, 
                Label = Resources.Intercamber.ResourceManager.GetString(x.LblHobby)
            }).ToList();
            if(useRecurse)
                res.ForEach(x => { x.Children = RecurseHobbiesList(all, x.Code, true); });
            return res;
        }


        #region hobbies
        [HttpPost]
        public JsonResult ListHobbies(string codeParent)
        {
            List<Hobbies> all = HobbiesHelper.AllHobbies;
            return Json(RecurseHobbiesList(all, codeParent, true), JsonRequestBehavior.AllowGet);
        }
        #endregion

    }
}
